package com.zhxt.web.controller.reportapi;

import com.zhxt.common.annotation.Log;
import com.zhxt.common.core.domain.AjaxResult;
import com.zhxt.common.enums.BusinessType;
import com.zhxt.system.domain.SysGw;
import com.zhxt.system.domain.SysTjcode;
import com.zhxt.system.domain.ZhxtLeader;
import com.zhxt.system.service.ILeaderSerivce;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 干部管理
 */

@RestController
@RequestMapping("/repapi/leader")
public class LeaderController {
    @Autowired
    private ILeaderSerivce iLeaderSerivce;
    /**
     * 查询数据最后一个月份
     */
    @GetMapping("/getMaxYearmon")
    public AjaxResult getMaxYearmon(){
        AjaxResult ajax = AjaxResult.success();
        String rYearmon=iLeaderSerivce.getMaxYearmon();
        ajax.put("yearmon", StringEscapeUtils.escapeHtml4(rYearmon));
        return ajax;
    }
    /**
     * 查询机构列表
     */
    @PostMapping("/getTjcodeList")
    public AjaxResult getTjcodeList(String tjcode){
        AjaxResult ajax = AjaxResult.success();
        List<SysTjcode> sysTjcodeList = iLeaderSerivce.getTjcodeList(tjcode);
        if (sysTjcodeList != null && !sysTjcodeList.isEmpty()) {
            // 对返回的列表进行XSS过滤
            sysTjcodeList.forEach(tj -> {
                tj.setTjcode(StringEscapeUtils.escapeHtml4(tj.getTjcode()));
                tj.setTjname(StringEscapeUtils.escapeHtml4(tj.getTjname()));
                tj.setUptjcode(StringEscapeUtils.escapeHtml4(tj.getUptjcode()));
            });
            ajax.put("hxwhList", sysTjcodeList);
        }
        return ajax;
    }

    /**
     * 机构、月份、姓名查询接口
     */
    @Log(title = "大屏-查看干部列表", businessType = BusinessType.OTHER)
    @PostMapping("/getLeaderList")
    public AjaxResult getLeaderList(String yearmon,String tjcode,String empname){
        AjaxResult ajax=AjaxResult.success();
        if(yearmon.isEmpty() || yearmon.equals("")){
            return AjaxResult.error("年月不能为空");
        }
        if(tjcode.isEmpty() || (tjcode.charAt(0)!='T' && tjcode.charAt(0)!='4')){
            ajax.put("tjname","河南");
        }else{
            SysTjcode sysTjcode=iLeaderSerivce.getTjName(tjcode);
            ajax.put("tjname",sysTjcode==null?"":sysTjcode.getTjname());
        }
        List<ZhxtLeader> zhxtLeaderList=iLeaderSerivce.getLeaderList(yearmon,tjcode,empname);
        ajax.put("data",zhxtLeaderList==null?new ArrayList<>():zhxtLeaderList);
        return ajax;
    }
    /**
     * 机构、月份、岗位历任信息查询接口
     */
    @Log(title = "大屏-查看岗位历史", businessType = BusinessType.OTHER)
    @PostMapping("getGwHistory")
    public AjaxResult getGwHistory(String tjcode,String gwname){
        AjaxResult ajax=AjaxResult.success();
        if(tjcode.isEmpty()){
            return AjaxResult.error("机构不能为空");
        }else if(gwname.isEmpty()){
            List<ZhxtLeader> zhxtLeaderList=iLeaderSerivce.getAllGwHistory(tjcode);
            return getAjaxResult(tjcode, ajax, zhxtLeaderList);
        }else{
            List<ZhxtLeader> zhxtLeaderList=iLeaderSerivce.getGwHistory(tjcode,gwname);
            return getAjaxResult(tjcode, ajax, zhxtLeaderList);
        }
    }

    private AjaxResult getAjaxResult(String tjcode, AjaxResult ajax, List<ZhxtLeader> zhxtLeaderList) {
        SysTjcode sysTjcode=iLeaderSerivce.getTjName(tjcode);
        if (sysTjcode!=null){
            ajax.put("tjname",sysTjcode);
        }
        if(!zhxtLeaderList.isEmpty()){
            ajax.put("data",zhxtLeaderList);
        }
        return ajax;
    }

    /**
     * 首页一把手列表信息
     */
    @PostMapping("getLeaderIndex")
    public AjaxResult getLeaderIndex(){
        AjaxResult ajax=AjaxResult.success();
        String yearmon=iLeaderSerivce.getMaxYearmon();
        if(yearmon.isEmpty()){
            return ajax;
        }
        List<ZhxtLeader> zhxtLeaderList=iLeaderSerivce.getLeaderIndex(yearmon);
        ajax.put("data",zhxtLeaderList);
        return ajax;
    }
}
